using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    public class PlayerInputController : MonoBehaviour
    {
        [SerializeField]
        private bool active = true;
        public bool Active
        {
            get => active;
            set => active = value;
        }

        [SerializeField]
        private bool canMove = true;
        public bool CanMove
        {
            get => canMove;
            set => canMove = value;
        }

        [SerializeField]
        private bool canChangeWeapon = true;
        public bool CanChangeWeapon
        {
            get => canChangeWeapon;
            set => canChangeWeapon = value;
        }
        
        public PlayerInput playInput;
        public PlayerAnimInput animInput;

        public Camera playerCamera;
        public Rigidbody rigidBody;
        public AIEntity aiEntity;
        
        [SerializeField]
        private List<BehaviorBase> behaviors;

        [SerializeField]
        private Vector3 lastDirection;
        public Vector3 LastDirection
        {
            get => lastDirection;
            set => lastDirection = value;
        }

        private void Awake()
        {
            if(!active) return;
            
            foreach (var behavior in behaviors)
            {
                if (behavior.Active)
                {
                    behavior.OnEnter();
                }
            }
        }

        private void Update()
        {
            if(!active) return;
            
            foreach (var behavior in behaviors)
            {
                if (behavior.Active)
                {
                    behavior.OnUpdate();
                }
            }
        }

        private void FixedUpdate()
        {
            if(!active) return;
            
            foreach (var behavior in behaviors)
            {
                if (behavior.Active)
                {
                    behavior.OnFixedUpdate();
                }
            }
        }

        private void LateUpdate()
        {
            if(!active) return;
            
            foreach (var behavior in behaviors)
            {
                if (behavior.Active)
                {
                    behavior.OnLateUpdate();
                }
            }
        }


        public void Repositioning(float turnSmoothing)
        {
            if(lastDirection != Vector3.zero && rigidBody)
            {
                lastDirection.y = 0;
                Quaternion targetRotation = Quaternion.LookRotation(lastDirection);
                Quaternion newRotation = Quaternion.Slerp(rigidBody.rotation, targetRotation, turnSmoothing);
                // rigidBody.MoveRotation(newRotation);
            }
        }
        

        public bool IsMoving()
        {
            return Mathf.Abs(playInput.HorRealValue) > 0.001f || Mathf.Abs(playInput.VerRealValue) > 0.001f;
        }
        
    }
}